package com.tronsis.distribution.activity.me.upgrade;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageAndVideoPreviewSeeActivity;
import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.ReturnDetailBean;
import com.tronsis.distribution.biz.factory.UpgradeBizFactory;
import com.tronsis.distribution.util.CommonUtil;
import com.tronsis.distribution.util.StateUtil;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.imageloader.ImageLoader;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.lzy.imagepicker.ImagePicker.REQUEST_CODE_PREVIEW;

/**
 * Allen: 退押金详情
 */
public class ReturnApplyDetailActivity extends ToolBarBaseActivity {
    @BindView(R.id.tv_type)
    TextView mTvType;
    @BindView(R.id.tv_level)
    TextView mTvLevel;
    @BindView(R.id.tv_retuen_money)
    TextView mTvRetuenMoney;
    @BindView(R.id.tv_state)
    TextView mTvStatus;
    @BindView(R.id.rv_user_upload)
    RecyclerView mRvUserUpload;
    private String id; //退押金ID
    private ArrayList<ImageItem> mImageList = new ArrayList<>();
    private CommonAdapter<ImageItem> mPayImageAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return_apply_detail);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.order_detail));
        initImageRv();
        id = getIntent().getStringExtra("id");
        httpGetRetrunDetail(id);
    }

    private void httpGetRetrunDetail(String id) {
        try {
            UpgradeBizFactory.createBiz().getReturnDetail(this, id, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    ReturnDetailBean mRetrunDetailBean = (ReturnDetailBean) data;
                    setDetailView(mRetrunDetailBean);

                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void setDetailView(ReturnDetailBean mRetrunDetailBean) {
        mTvType.setText("退还押金");
//        mTvLevel.setText(mRetrunDetailBean.getLevel() + "级");
        mTvLevel.setText(StateUtil.getLevelText(mRetrunDetailBean.getLevel()));

        mTvRetuenMoney.setText(getString(R.string.format_price, mRetrunDetailBean.getDeposit()));
        if (mRetrunDetailBean.getDepositFid() != null) {
            ArrayList<ImageItem> splitImage = CommonUtil.getSplitImage(mRetrunDetailBean.getDepositFid());
            mImageList.clear();
            mImageList.addAll(splitImage);
            mPayImageAdapter.notifyDataSetChanged();
        }
    }

    private void initImageRv() {
        mPayImageAdapter = new CommonAdapter<ImageItem>(mContext, mImageList, R.layout.item_grid_image_view) {
            @Override
            protected void convert(final ViewHolder holder, final ImageItem imageItem, final int position) {
                ImageView ivItem = (ImageView) holder.getView(R.id.iv_item);
                if (TextUtils.isEmpty(imageItem.url)) {
                    ImageLoader.display(mContext, new File(imageItem.path), ivItem);
                }

                if (TextUtils.isEmpty(imageItem.path)) {
                    ImageLoader.display(mContext, imageItem.url, ivItem);
                }

                ivItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(mImageList.get(position).path) && TextUtils.isEmpty(mImageList.get(position).url)) {
                            return;
                        }
                        Intent intentPreview = new Intent(mContext, ImageAndVideoPreviewSeeActivity.class);
                        intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                        intentPreview.putParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS, mImageList);
                        startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
                    }
                });
            }
        };

        mRvUserUpload.setLayoutManager(new GridLayoutManager(mContext, 3, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvUserUpload.setAdapter(mPayImageAdapter);
        mRvUserUpload.setHasFixedSize(true);
        mRvUserUpload.setNestedScrollingEnabled(false);
    }
}
